package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cc.orange.databinding.FragmentMsgBinding;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.adapter.MsgAdapter;
import cc.orange.base.BaseFragment;
import cc.orange.entity.ChanListEntity;
import cc.orange.entity.UnMsgEntity;
import cc.orange.http.ApiService;

import cc.orange.R;

import cc.orange.entity.IsLoginsEntity;
import cc.orange.utils.ZXToastUtil;
import demo.smart.access.xutlis.util.ZXAppUtil;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;

public class MsgFragment extends BaseFragment {

    private MsgAdapter adapter;
    private List<ChanListEntity.Data>list = new ArrayList<>();
    private FragmentMsgBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_msg,container,false);
        initViews();
        chatList();
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHightS(UnMsgEntity entity){//未读消息
        if(!TextUtils.isEmpty(entity.getMsg_num()) && !entity.getMsg_num().equals("0")){
            binding.homeText3Num.setVisibility(View.VISIBLE);
            binding.homeText3Num.setText(entity.getMsg_num());
        }else{
            binding.homeText3Num.setText("0");
            binding.homeText3Num.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHight(IsLoginsEntity entity){
        if(entity.isLogins()){
            list.clear();
            chatList();
        }else{
            list.clear();
            binding.llNone.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews() {
//        //下拉刷新
//        binding.smartRL.setRefreshHeader(new ClassicsHeader(getActivity()));
//        binding.smartRL.setRefreshFooter(new ClassicsFooter(getActivity()));
//        binding.smartRL.setEnableLoadMore(true);
//        binding.smartRL.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                list.clear();
//                chatList();
//                binding.smartRL.finishRefresh(3);
//            }
//        });
//        binding.smartRL.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                binding.smartRL.finishLoadMore(3);
//            }
//        });

        binding.msgRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));

        adapter = new MsgAdapter(list);
        binding.msgRecycler.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(list.size() == 0)return;
                if(list.get(position).getWomanId().equals("666666")){
                    Intent intent2 = new Intent(getActivity(), ConversatioSystemActivity.class);
                    intent2.putExtra("uid",""+list.get(position).getWomanId());
                    intent2.putExtra("name",""+list.get(position).getName());
                    startActivity(intent2);

                }else{
                    Intent intent = new Intent(getActivity(), ConversationListActivity.class);
                    intent.putExtra("uid",""+list.get(position).getWomanId());
                    intent.putExtra("name",""+list.get(position).getName());
                    startActivity(intent);
                }
            }
        });


        //通话记录
        binding.msgRels1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                boolean isLogin = ss.getBool("isLogin_talk",false);
                if(!isLogin)return;
                Intent intent = new Intent(getActivity(), HistoryListActivity2.class);
                startActivity(intent);
            }
        });

        //系统消息
        binding.msgRels2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                boolean isLogin = ss.getBool("isLogin_talk",false);
                if(!isLogin)return;
//                Intent intent = new Intent(getActivity(), Conversation4KFActivity.class);
//                intent.putExtra("type_face",12);
//                startActivity(intent);
                Intent intent2 = new Intent(getActivity(), ConversatioSystemActivity.class);
                intent2.putExtra("uid","666666");
                intent2.putExtra("name","系统消息");
                startActivity(intent2);
            }
        });


        //客服消息
        binding.msgRels3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                boolean isLogin = ss.getBool("isLogin_talk",false);
                if(!isLogin)return;
                Intent intent = new Intent(getActivity(), Conversation4KFActivity.class);
                startActivity(intent);
            }
        });
    }

    private void chatList(){
        RetrofitFactory.create(ApiService.class)
                .chatList(getTokens(), ZXAppUtil.getPackageName())
                .enqueue(new DefaultCallback<ChanListEntity>() {
                    @Override
                    public void onStart(Call<ChanListEntity> call) {
//                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<ChanListEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<ChanListEntity> call, ChanListEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.addAll(baseEntity.getData());
                            if(list.size() == 0){
                                binding.llNone.setVisibility(View.VISIBLE);
                            }else{
                                binding.llNone.setVisibility(View.GONE);
                            }
                            for (int i = 0; i < list.size(); i++) {
                                if(list.get(i).getWomanId().equals("666666")){
                                    EventBus.getDefault().post(new UnMsgEntity(""+list.get(i).getUnReadNum()));
                                }
                            }
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
    }
}